﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using ChessKit.ChessLogics;
using ChessKit.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ChessKit.UnitTests.Chess_Logics
{
  [TestClass]
  public class Fen
  {
    const string LibFile = @"c:\Users\Ilya\Documents\Visual Studio 2010\Projects\FenDownloader\FenDownloader\positions.fen";

    [TestMethod, Ignore]
    public void FromFemStringPerformance()
    {
      // Current result 2.7μs
      const int times = 20;

      var lines = File.ReadAllLines(LibFile);
      var sw = Stopwatch.StartNew();
      for (int j = 0; j < times; j++)
        for (int i = 0; i < lines.Length; i++)
          Board.FromFenString(lines[i]);
      sw.Stop();
      Console.WriteLine("Average FEN load time is: {0:F2}μs", 
        sw.Elapsed.TotalSeconds /times * 1e6 / lines.Length);
    }

    [TestMethod, Ignore]
    public void ToFenStringPerformance()
    {
      // Current result 5μs
      var boards = File.ReadAllLines(LibFile)
        .Select(Board.FromFenString)
        .ToArray();

      const int times = 20;

      var sw = Stopwatch.StartNew();
      
      for (int j = 0; j < times; j++)
        for (int i = 0; i < boards.Length; i++)
          boards[i].ToFenString();

      sw.Stop();

      Console.WriteLine("Average FEN save time is: {0:F2}μs", 
        sw.Elapsed.TotalSeconds /times * 1e6 / boards.Length);
    }

    [TestMethod, Ignore]
    public void AverageFenStringLength()
    {
      var lines = File.ReadAllLines(LibFile);
      Console.WriteLine(lines.Average(s => s.Length));
      Console.WriteLine(lines.Max(s => s.Length));
      Console.WriteLine(lines.Min(s => s.Length));
    }

    [TestMethod]
    public void Validity()
    {
      foreach (var expected in File.ReadAllLines(LibFile))
      {
        var board = Board.FromFenString(expected);
        Assert.AreEqual(expected, board.ToFenString());
      }
    }

    [TestMethod]
    public void StartPosition()
    {
      Assert.AreEqual(4, Pos.Pack(Position.Parse("e1")));
      Assert.AreEqual(Pt.K, Board.StartPosition.GetPieceAt(Position.Parse("e1")));
    }
  }
}
